//
//  ASTodayTransFromViewController.m
//  TestPrj
//
//  Created by 朱铅峰 on 2019/12/19.
//  Copyright © 2019 Xing. All rights reserved.
//

#import "ASTodayTransFromViewController.h"
#import "ASTodayTransFromListCell.h"
#import "ASTodayTransToViewController.h"

@interface ASTodayTransFromViewController ()<UINavigationControllerDelegate,UIViewControllerAnimatedTransitioning>

@end

@implementation ASTodayTransFromViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.title = @"AppStore中Today的转场";
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[ASTodayTransFromListCell class] forCellReuseIdentifier:@"cell"];
}

- (void)viewDidAppear:(BOOL)animated {
    
    [super viewDidAppear:animated];
    
    self.navigationController.delegate = self;
}

- (void)viewDidDisappear:(BOOL)animated {
    
    [super viewDidDisappear:animated];
    
    self.navigationController.delegate = nil;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ASTodayTransFromListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return UITableViewAutomaticDimension;
}

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [UIView animateWithDuration:0.3 animations:^{
        cell.transform = CGAffineTransformMakeScale(0.85, 0.85);
    }];
    
    return YES;
}

- (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [UIView animateWithDuration:0.3 animations:^{
        cell.transform = CGAffineTransformMakeScale(1, 1);
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ASTodayTransToViewController *vc = [[ASTodayTransToViewController alloc] init];
    vc.fromIndexPath = indexPath;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - UINavigationControllerDelegate

- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC {
    
    return self;
}

#pragma mark - UIViewControllerAnimatedTransitioning

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
    
    return 1.0;
}

- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {
    
    if ([transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey] != self) {
        return;
    }
    
    //获取的转场视图容器
    UIView *containerView = [transitionContext containerView];
    
    //获取的B控制器
    ASTodayTransToViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *toBgImageView = toVC.imageView;
    toBgImageView.hidden = YES;
    toVC.view.alpha = 0;
    //将B添加到转场视图容器里
    [containerView addSubview:toVC.view];
     
    //获取对应的A页面的cell
    ASTodayTransFromListCell *cell = (ASTodayTransFromListCell *)[self.tableView cellForRowAtIndexPath:[self.tableView indexPathForSelectedRow]];
    UIView *fromBgImageView = [[UIImageView alloc]initWithImage:cell.iconView.image];
    fromBgImageView.frame = [containerView convertRect:cell.iconView.frame fromView:cell.iconView.superview];
    
    //创建A页面的cell的标签，设置frame
    UILabel *typeL = [self copyLabel:cell.nameLabel];
    [fromBgImageView addSubview:typeL];
    
    //创建关闭按钮，虽然A页面没有该元素，但动画时会出现，这里的frame可以根据自己感觉调整
    UIButton *closeBtn = [UIButton buttonWithType:0];
    closeBtn.x = cell.width - 2*15 - 60;
    closeBtn.centerY = cell.height/2;
    closeBtn.size = CGSizeMake(30, 30);
    closeBtn.alpha = 0.5;
    [closeBtn setBackgroundImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
    [fromBgImageView addSubview:closeBtn];
    
    //将A添加到转场视图容器里,注意，需要先添加B在添加A，应该A是在前面做动画的
    [containerView addSubview:fromBgImageView];
    
    //这里可以设置动画时间，弹性等
    [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0.0f usingSpringWithDamping:0.6f initialSpringVelocity:1.0f options:UIViewAnimationOptionCurveLinear animations:^{
        
        [containerView layoutIfNeeded];
        toVC.view.alpha = 1.0f;
        
        //这里将B页面各个元素的frame给A页面对应的
        typeL.frame = toVC.nameLabel.frame ;
        closeBtn.frame = toVC.closeControl.frame;
        fromBgImageView.frame = [containerView convertRect:toBgImageView.frame fromView:toBgImageView.superview];
        
    } completion:^(BOOL finished) {
        
        toBgImageView.hidden = NO;
        closeBtn.hidden = YES;
        [fromBgImageView removeFromSuperview];
        [transitionContext completeTransition:!transitionContext.transitionWasCancelled];
    }];
}

- (UILabel *)copyLabel:(UILabel *)label {
    
    UILabel *newLabel = [[UILabel alloc] init];
    newLabel.text = label.text;
    newLabel.font = label.font;
    newLabel.alpha = label.alpha;
    newLabel.frame =  label.frame;
    newLabel.textColor = label.textColor;
    newLabel.textAlignment = label.textAlignment;
    
    return newLabel;
}

@end
